package com.wenwu.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wenwu.common.bean.RelicsMWenwu;
import com.wenwu.common.exception.RelicsException;
import com.wenwu.common.page.PaginationVO;
import com.wenwu.dao.RelicsMWenwuMapper;
import com.wenwu.service.MoveableRegisterService;
@Service
public class MoveableRegisterServiceImpl implements MoveableRegisterService {

	
	@Autowired
	private RelicsMWenwuMapper relicsWenwuMapper;
	
	public String addObj(RelicsMWenwu relicsMWenwu) throws RelicsException {
		try{
			String reuslt = "";
			if(relicsMWenwu.getmRelicsId() != null){
				int num = relicsWenwuMapper.updateByPrimaryKeySelective(relicsMWenwu);
				if(num > 0){
					reuslt = "修改成功！";
				}else{
					reuslt = "修改失败！";
				}
			}else{
				int num = relicsWenwuMapper.insertSelective(relicsMWenwu);
				if(num > 0){
					reuslt = "添加成功！";
				}else{
					reuslt = "添加失败！";
				}
			}
			return reuslt;
		}catch(Exception e){
			throw new RelicsException("系统错误，操作失败",e);
		}
		
	}

	public PaginationVO selectByCondition(RelicsMWenwu relicsMWenwu,PaginationVO pageVo)
			throws RelicsException {
		try {
			List<RelicsMWenwu> list = relicsWenwuMapper.selectListByPage(relicsMWenwu, pageVo);
			int count = relicsWenwuMapper.selectTotalRecord(relicsMWenwu);
			pageVo.setList(list);
			pageVo.setTotalRecord(count);
			return pageVo;
		} catch (Exception e) {
			throw new RelicsException("系统错误，请稍后再试", e);
		}
	}

	public String deleteBykeys(String keys) throws RelicsException {
		String result = "删除失败！";
		if (keys != null) {
			try {
				List<String> keyList = new ArrayList<String>();
				String[] objs = keys.split(",");
				for (String k : objs) {
					keyList.add(k);
				}
				result = "成功删除" + relicsWenwuMapper.deleteInfoByKey(keyList) + "条数据";
			} catch (Exception e) {
				throw new RelicsException("系统错误，请稍后再试", e);
			}
		}
		return result;
	}

	public RelicsMWenwu selectObjByKey(Integer key) throws RelicsException {
		try {
			return relicsWenwuMapper.selectByPrimaryKey(key);
		} catch (Exception e) {
			throw new RelicsException("系统错误，请稍后再试", e);
		}
	}

	

}
